home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / Java / JavaRadioStation / Source / com / apple / jens / radio / DJ.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  6.4 KB  |  191 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        DJ.java
  3.     
  4.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  5.     
  6.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  7.                 ("Apple") in consideration of your agreement to the following terms, and your
  8.                 use, installation, modification or redistribution of this Apple software
  9.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  10.                 please do not use, install, modify or redistribute this Apple software.
  11.  
  12.                 In consideration of your agreement to abide by the following terms, and subject
  13.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  14.                 copyrights in this original Apple software (the "Apple Software"), to use,
  15.                 reproduce, modify and redistribute the Apple Software, with or without
  16.                 modifications, in source and/or binary forms; provided that if you redistribute
  17.                 the Apple Software in its entirety and without modifications, you must retain
  18.                 this notice and the following text and disclaimers in all such redistributions of
  19.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  20.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  21.                 Apple Software without specific prior written permission from Apple.  Except as
  22.                 expressly stated in this notice, no other rights or licenses, express or implied,
  23.                 are granted by Apple herein, including but not limited to any patent rights that
  24.                 may be infringed by your derivative works or by other works in which the Apple
  25.                 Software may be incorporated.
  26.  
  27.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  28.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  29.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  31.                 COMBINATION WITH YOUR PRODUCTS.
  32.  
  33.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  34.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  37.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  38.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  39.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.                 
  41.     Change History (most recent first):
  42.  
  43. */
  44.  
  45.  
  46. package com.apple.jens.radio;
  47.  
  48. import java.io.*;
  49.  
  50.  
  51. /** An abstract base class that generates MP3-format Buffers to be streamed by Transmitters.
  52.     A DJ subclass might get the data from local MP3 files (like FileDJ)
  53.     or by relaying a stream from a remote MP3 server.
  54.     @see FileDJ
  55. */
  56.     
  57. public abstract class DJ extends Thread {
  58.  
  59.     /** Creates a DJ thread but doesn't start it yet. */
  60.     public DJ( ) {
  61.         super("DJ");
  62.     }
  63.     
  64.     
  65.     /** Sets the DJ's Station -- this is done once, when the DJ is created. */
  66.     public void setStation( Station station ) {
  67.         fStation = station;
  68.         setName(station.getName()+"'s DJ");
  69.  
  70.         int bufferCount = station.getProperties().getIntProperty(kPropBufferCount,kDefaultBufferCount);
  71.         fBuffers = new Buffer[bufferCount];
  72.         // The array elements start out null since no buffers have been read yet.
  73.     }
  74.     
  75.     
  76.     public Station getStation( ) {
  77.         return fStation;
  78.     }
  79.     
  80.     
  81.     // BUFFER ACCESS:
  82.     
  83.     
  84.     //FIX: A better design for this would be to implement a BufferIterator,
  85.     // to spare clients from having to deal with numeric indices.
  86.     
  87.     
  88.     /** Returns the buffer index for a Transmitter to start with */
  89.     int getFirstBufferIndex( ) {
  90.         // Start 'em halfway around the ring to minimize chances of collision:
  91.         return (fCurBuf + fBuffers.length/2) % fBuffers.length;
  92.     }
  93.     
  94.     
  95.     /** Returns the nth buffer counting around the buffer ring
  96.         (using modulo arithmetic to index into the ring array.)
  97.         This method is for use by Transmitters (readers), <i>not</i> by
  98.         DJ subclasses. It will block if the nth buffer is currently
  99.         being written to by the DJ, thus preventing Transmitters from
  100.         overtaking the DJ. */
  101.     Buffer getFilledBuffer( int n ) throws InterruptedException {
  102.         if( n < 0 ) throw new IllegalArgumentException(Integer.toString(n));
  103.         n = n % fBuffers.length;
  104.         
  105.         synchronized(this) {
  106.             // Block until the buffer's been created and is no longer in use by the DJ:
  107.             while( n == fCurBuf || fBuffers[n]==null )
  108.                 this.wait();        // Wait for getNextFreeBuffer to notify
  109.         }
  110.  
  111.         return fBuffers[n];
  112.     }
  113.     
  114.     
  115.     /** Returns the next available buffer for the DJ to write to.
  116.         This method should be called only by the subclass's <code>spin</code> method. */
  117.     protected Buffer getNextFreeBuffer( ) {
  118.         synchronized(this) {
  119.             ++fCurBuf;
  120.             if( fCurBuf == fBuffers.length )
  121.                 fCurBuf = 0;
  122.             this.notifyAll();    // wake up Transmitters blocked in getBuffer method
  123.         }
  124.         
  125.         Buffer next = fBuffers[fCurBuf];
  126.         if( next == null )        // Allocate buffers 1st time around
  127.             next = fBuffers[fCurBuf] = new Buffer(fStation.getStationName()+"#"+fCurBuf);
  128.         return next;
  129.     }
  130.     
  131.     
  132.     // RUN-LOOP:
  133.     
  134.     
  135.     /** Should be overridden by the caller to spin some tunes and generate
  136.         some Buffers via setCurrentBuffer. */
  137.     protected abstract void spin( ) throws IOException, InterruptedException;
  138.     
  139.     
  140.     /** The DJ thread's run loop.
  141.         Don't call this directly -- the DJ's owner should call start instead.
  142.         @see Station */
  143.     public void run( ) {
  144.         try{
  145.             while(true) {
  146.                 spin();
  147.             }
  148.         }catch( IOException x ) {
  149.             Radio.WARN(1,this,"Caught a fatal IOException, exiting...");
  150.             x.printStackTrace(System.err);
  151.         }catch( InterruptedException ix ) {
  152.             LOG(1,"Interrupted! Exiting...");
  153.         }
  154.         //FIX: When this thread exits, all Transmitters must be shut down
  155.         //        or they will block forever...
  156.     }
  157.     
  158.     
  159.     protected void LOG( int level, String msg ) {
  160.         Radio.LOG(level,getName(),msg);
  161.     }
  162.     
  163.     
  164.     // INSTANCE DATA:
  165.  
  166.  
  167.     private Station fStation;
  168.     
  169.     /** The circular ring of buffers used by the DJ and Transmitters. */
  170.     private Buffer[] fBuffers;
  171.     
  172.     /** The index into fBuffers of the buffer the DJ is currently writing to.
  173.         The buffer with this index will never be handed out
  174.         to Transmitters -- they'll block until fCurBuf is incremented. */
  175.     private int fCurBuf = -1;
  176.     
  177.     
  178.     // STATIC DATA:
  179.     
  180.     
  181.     /** The default number of buffers, if not overridden in Defaults or station prop files. */
  182.     public static final int kDefaultBufferCount = 6;
  183.     
  184.     
  185.     // PROPERTIES:
  186.     
  187.     
  188.     public static final String kPropBufferCount = "buffer-count";
  189.  
  190. }
  191.